/**
 * 根据id找到值
 * @param roots {Array}
 * @param value 比对的值
 * @param config
 * @returns {string}
 */
export function getTreeNameById(roots, value,
  config = {
    text: 'text',
    value: 'value',
    children: 'children'
  }) {
  var node = getTreeNodeById(roots, value, config)
  return node === undefined ? '' : node[config.text]
}

/**
 * 根据id找到值
 * @param roots {Array}
 * @param value 比对的值
 * @param config
 * @returns {string}
 */
export function getTreeNodeById(roots, value,
  config = {
    text: 'text',
    value: 'value',
    children: 'children'
  }) {
  const rootMap = {}
  deepTreeToMap(roots, rootMap, config)
  // console.log(JSON.stringify(rootMap))
  return rootMap[`root${value}`]
}

/**
 * 根据id找到值
 * @param roots {Array}
 * @param config {Object}
 * @returns {Object}
 */
export function treeToMap(roots, config = {
  text: 'text',
  value: 'value',
  children: 'children'
}) {
  const rootMap = {}
  deepTreeToMap(roots, rootMap, config)
  // console.log(JSON.stringify(rootMap))
  return rootMap
}

function deepTreeToMap(root, rootMap, config) {
  root.forEach(node => {
    rootMap[`root${node[config.value]}`] = node
    if (node[config.children] && node[config.children].length > 0) {
      deepTreeToMap(node[config.children], rootMap, config)
    }
  })
}

/**
 * 深度克隆对象
 * @param obj
 * @returns {{}}
 */
export function clone(obj) {
  var o
  // 如果  他是对象object的话  , 因为null,object,array  也是'object';
  if (typeof obj === 'object') {
    // 如果  他是空的话
    if (obj === null) {
      o = null
    } else {
      // 如果  他是数组arr的话
      if (obj instanceof Array) {
        o = []
        for (var i = 0, len = obj.length; i < len; i++) {
          o.push(clone(obj[i]))
        }
      } else {
        // 如果  他是对象object的话
        o = {}
        for (var j in obj) {
          o[j] = clone(obj[j])
        }
      }
    }
  } else {
    o = obj
  }
  return o
}

export function uuid() {
  const s = []
  const hexDigits = '0123456789abcdef'
  for (var i = 0; i < 36; i++) {
    s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1)
  }
  s[14] = '4' // bits 12-15 of the time_hi_and_version field to 0010
  s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1) // bits 6-7 of the clock_seq_hi_and_reserved to 01
  s[8] = s[13] = s[18] = s[23] = '-'

  const uuid = s.join('').replace(/-/g, '')
  return uuid
}

/**
 * @description 格式化金额
 * @param number：要格式化的数字
 * @param decimals：保留几位小数 默认0位
 * @param decPoint：小数点符号 默认.
 * @param thousandsSep：千分位符号 默认为,
 */
export function formatMoney(number, decimals = 0, decPoint = '.', thousandsSep = '') {
  number = (number + '').replace(/[^0-9+-Ee.]/g, '')
  const n = !isFinite(+number) ? 0 : +number
  const prec = !isFinite(+decimals) ? 0 : Math.abs(decimals)
  const sep = (typeof thousandsSep === 'undefined') ? ',' : thousandsSep
  const dec = (typeof decPoint === 'undefined') ? '.' : decPoint
  let s = ''
  const toFixedFix = function(n, prec) {
    const k = Math.pow(10, prec)
    return '' + Math.ceil(n * k) / k
  }
  s = (prec ? toFixedFix(n, prec) : '' + Math.round(n)).split('.')
  const re = /(-?\d+)(\d{3})/
  while (re.test(s[0])) {
    s[0] = s[0].replace(re, '$1' + sep + '$2')
  }
  if ((s[1] || '').length < prec) {
    s[1] = s[1] || ''
    s[1] += new Array(prec - s[1].length + 1).join('0')
  }
  return s.join(dec)
}

/**
 * @description 格式化小数
 * @param number：要格式化的数字
 * @param decimals：保留几位小数 默认0位
 * @param decPoint：小数点符号 默认.
 * @param thousandsSep：千分位符号 默认为,
 */
export function formatDecimals(number, decimals = 2, decPoint = '.', thousandsSep = '') {
  return parseFloat(formatMoney(number, decimals, '.', ''))
}

export function toChinesNum(num) {
  const changeNum = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九']
  const unit = ['', '十', '百', '千', '万']
  num = parseInt(num)
  const getWan = (temp) => {
    const strArr = temp.toString().split('').reverse()
    let newNum = ''
    for (var i = 0; i < strArr.length; i++) {
      newNum = (i == 0 && strArr[i] == 0 ? '' : (i > 0 && strArr[i] == 0 && strArr[i - 1] == 0 ? '' : changeNum[strArr[i]] + (strArr[i] == 0 ? unit[0] : unit[i]))) + newNum
    }
    return newNum
  }
  const overWan = Math.floor(num / 10000)
  let noWan = num % 10000
  if (noWan.toString().length < 4) {
    noWan = '0' + noWan
  }
  return overWan ? getWan(overWan) + '万' + getWan(noWan) : getWan(num)
}
